using System;
using System.Collections.Generic;
using System.Text;

using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework;
using CreamX.Framework.Game.Components;
using CreamX.Framework.Attributes;

namespace CreamXComponentPack
{
    [CreamXComponentAttribute("PlayerControllerComponent")]
    public class PlayerControllerComponent:CreamXComponent
    {
        GamePadState _currentPadState, _lastPadState;
        KeyboardState _currentKBState, _lastKBState;
        public PlayerControllerComponent()
        {
        }
        public override void OnRegister()
        {
            Enabled = true;
            base.OnRegister();
        }
        private int _playerNumber=1;
        [CreamXComponentProperty("PlayerNumber")]
        public int PlayerNumber
        {
            get { return _playerNumber; }
            set {if(value>0 &&value<4) _playerNumber = value; }
        }
        public override void Update(float elapsed)
        {
            _lastKBState = _currentKBState;
            _currentKBState = Keyboard.GetState();
            _lastPadState = _currentPadState;
            _currentPadState = GamePad.GetState((Microsoft.Xna.Framework.PlayerIndex)PlayerNumber - 1);
            base.Update(elapsed);
        }
        public GamePadState CurrentGamePadState
        {
            get { return _currentPadState; }
        }
        public GamePadState LastGamePadState
        {
            get { return _lastPadState; }
        }
        public Vector2 LeftStickVector
        {
            get
            {
                return new Vector2(CurrentGamePadState.ThumbSticks.Left.X,
                CurrentGamePadState.ThumbSticks.Left.Y);
            }
        }
        public Vector2 RightStickVector
        {
            get
            {
                return new Vector2(CurrentGamePadState.ThumbSticks.Right.X,
                CurrentGamePadState.ThumbSticks.Right.Y);
            }
        }
        public bool IsKeyDown(Keys key)
        {
            return _currentKBState.IsKeyDown(key);
        }
        public bool IsNewButtonPress(Buttons button)
        {
            return _currentPadState.IsButtonDown(button) && 
                !_lastPadState.IsButtonDown(button);
        }
    }
}
